var bcrypt           = require('bcrypt-nodejs');

module.exports = {
    generateHash : function(password) {
        return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
    },
    compareHashedPassword : function(plainPwd, hashedPwd){
        return bcrypt.compareSync(plainPwd, hashedPwd);
    },
    // route middleware to make sure a user is logged in
    isLoggedIn : function(req, res, next) {
        // if user is authenticated in the session, carry on
        if (req.isAuthenticated())
            return next();
        // if they aren't redirect them to the home page
        res.redirect('/');
    },
    // route middleware to make sure a user is admin
    isAdmin : function(req, res, next) {
        // if user is authenticated in the session, carry on
        if (req.isAuthenticated() && req.user.role === "ADMIN")
            return next();
        // if they aren't redirect them to the home page
        res.redirect('/');
    }
};